package cn.waka.sys.admin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Star;
import cn.waka.sys.admin.object.Starimg;

@Component
public class StarDao extends BaseDao {

    public List<Star> findList(Page p, Map<String, String> param) {
        int page = p.getPage();
        int start = (page - 1) * p.getSize();

        String sql = "select id,name,sex from s_star where 1=1 ";
        Set<String> pks = param.keySet();
        Object[] dbparams = new Object[2 + param.size()];
        int i = 0;
        if (param.size() > 0) {
            for (String pa : pks) {
                sql += " and " + pa + " like ?";
                dbparams[i] = param.get(pa);
                i++;
            }

            dbparams[param.size()] = start;
            dbparams[param.size() + 1] = p.getSize();
        } else {
            dbparams[0] = start;
            dbparams[1] = p.getSize();
        }
        sql += " order by id desc limit ?,?";
        List<Star> list = this.query(sql, new RowMapper<Star>() {
            @Override
            public Star mapRow(ResultSet rs, int rowNum) throws SQLException {
                Star b = new Star();
                b.setId(rs.getInt(1));
                b.setName(rs.getString(2));
                b.setSex(rs.getInt(3));
                return b;
            }

        }, dbparams);
        return list;
    }

    public int findCount(Page p, Map<String, String> param) {
        String sql = "select count(1) from s_star where 1=1 ";
        Set<String> pks = param.keySet();
        Object[] dbparams = new Object[param.size()];
        int i = 0;
        if (param.size() > 0) {
            for (String pa : pks) {
                sql += " and " + pa + " like ?";
                dbparams[i] = param.get(pa);
                i++;
            }
        }
        int c = 0;
        if (param.size() > 0) {
            c = this.queryForObject(sql, Integer.class, dbparams);
        } else {
            c = this.queryForObject(sql, Integer.class);
        }
        return c;
    }

    public List<Starimg> getStarImgs(int starId) {
        String sql = "select id,img,star_id from s_star_img where star_id=? ";
        List<Starimg> list = this.query(sql, new RowMapper<Starimg>() {
            @Override
            public Starimg mapRow(ResultSet rs, int rowNum) throws SQLException {
                Starimg b = new Starimg();
                b.setId(rs.getInt(1));
                b.setImg(rs.getString(2));
                b.setStarId(rs.getInt(3));
                return b;
            }

        }, starId);
        return list;
    }

    public Star findById(int id) {
        String sql = "select id,`name`,sex from s_star where id=?";
        List<Star> list = this.query(sql, new RowMapper<Star>() {
            @Override
            public Star mapRow(ResultSet rs, int rowNum) throws SQLException {
                Star b = new Star();
                b.setId(rs.getInt(1));
                b.setName(rs.getString(2));
                b.setSex(rs.getInt(3));
                return b;
            }

        }, id);
        return list.size() > 0 ? list.get(0) : null;
    }

    public int insert(final Star r) {
        KeyHolder holder = new GeneratedKeyHolder();
        this.update(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                String sql = "insert into s_star(`name`,sex)values(?,?)";
                PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, r.getName());
                ps.setInt(2, r.getSex());
                return ps;
            }
        }, holder);
        int id = holder.getKey().intValue();
        return id;
    }

    public int updateStar(Star r) {
        int rs = this.update("update s_star set `name`=?,sex=? where id=?",
                r.getName(),
                r.getSex(),
                r.getId());
        return rs;
    }

    public int del(int id) {
        int r = this.update("delete from s_star where id=?", id);
        return r;
    }

    public int delImg(int starId) {
        int r = this.update("delete from s_star_img where star_id=?", starId);
        return r;
    }

    public int delStarImg(int id) {
        int r = this.update("delete from s_star_img where id=?", id);
        return r;
    }

    public int saveImg(int starId, String img) {
        KeyHolder holder = new GeneratedKeyHolder();
        this.update(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                String sql = "insert into s_star_img(`star_id`,img)values(?,?)";
                PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setInt(1, starId);
                ps.setString(2, img);
                return ps;
            }
        }, holder);
        int id = holder.getKey().intValue();
        return id;
    }
}
